package yeqiang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yeqiang.service.IP2LocationService;
import yeqiang.vo.LocationVO;
import yeqiang.vo.R;

/**
 * @author yeqiang
 * @since 2021/6/2 下午2:54
 */
@RestController
public class IP2LocationController {

    @Autowired
    IP2LocationService ip2LocationService;

    /**
     * 导入数据
     * @return
     */
    @RequestMapping("/importIPDatabase")
    public R importIPDataBase() {
        R r = new R();
        ip2LocationService.init();
        r.setCode(R.ERROR_CODE_SUCC);
        r.setMsg("OK");
        return r;
    }

    /**
     * 根据ip地址查询国家信息
     * @param ip ip地址
     * @return 国家信息，包含英文国家简称，英文国家名
     */
    @RequestMapping("/ip2location")
    public R ip2location(@RequestParam("ip") String ip) {
        R r = new R();
        LocationVO locationVO = ip2LocationService.ip2location(ip);
        if (locationVO != null) {
            r.setCode(R.ERROR_CODE_SUCC);
            r.setMsg("OK");
            r.setData(locationVO);
        } else {
            r.setCode(R.ERROR_CODE_FAIL);
            r.setMsg("FAIL");
        }
        return r;
    }
}
